package com.common.MultiColumnListView.view;

import android.content.Context;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.RotateAnimation;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.starappallcommon.R;
/**
 * 列表头部下拉刷新文件
 * 
 * <br>Created 2014-6-6 下午5:25:39
 * @version  
 * @author   cyw    		
 *
 * @see
 */
public class XListViewHeader extends LinearLayout {
	private LinearLayout mContainer;
	private ImageView mArrowImageView;
	private ProgressBar mProgressBar;
	private TextView mHintTextView;
	private int mState = STATE_NORMAL;

	private Animation mRotateUpAnim;
	private Animation mRotateDownAnim;
	
	private final int ROTATE_ANIM_DURATION = 180;
	
	public final static int STATE_NORMAL = 0;
	public final static int STATE_READY = 1;
	public final static int STATE_REFRESHING = 2;

	/**
	 * 
	 * Creates a new instance of XListViewHeader.
	 * <br>Created 2014-6-9 下午5:46:19
	 * @param context 上下文
	 */
	public XListViewHeader(Context context) {
		super(context);
		initView(context);
	}

	/**
	 * 
	 * Creates a new instance of XListViewHeader.
	 * <br>Created 2014-6-6 下午5:30:52
	 * @param context  上下文
	 * @param attrs 属性集
	 */
	public XListViewHeader(Context context, AttributeSet attrs) {
		super(context, attrs);
		initView(context);
	}

	/**
	 * 初始化view
	 * 
	 * <br>Created 2014-6-9 下午5:46:40
	 * @param context 上下文
	 * @author        :  cyw
	 */
	private void initView(Context context) {
		// 初始情况，设置下拉刷新view高度为0
		LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
				LayoutParams.FILL_PARENT, 0);
		mContainer = (LinearLayout) LayoutInflater.from(context).inflate(
				R.layout.wishwall_xlistview_header, null);
		addView(mContainer, lp);
		setGravity(Gravity.CENTER);

		mArrowImageView = (ImageView)findViewById(R.id.xlistview_header_arrow);
		mHintTextView = (TextView)findViewById(R.id.xlistview_header_hint_textview);
		mProgressBar = (ProgressBar)findViewById(R.id.xlistview_header_progressbar);
		final float fromDegrees = 0.0f;
		final float toDegrees = -180.0f;
		final float pivotXValue = 0.5f;
		final float pivotYValue = 0.5f;
		mRotateUpAnim = new RotateAnimation(fromDegrees, toDegrees,
				Animation.RELATIVE_TO_SELF, pivotXValue, Animation.RELATIVE_TO_SELF,
				pivotYValue);
		mRotateUpAnim.setDuration(ROTATE_ANIM_DURATION);
		mRotateUpAnim.setFillAfter(true);
		final float DownfromDegrees = -180.0f;
        final float DowntoDegrees = 0.0f;
		mRotateDownAnim = new RotateAnimation(DownfromDegrees, DowntoDegrees,
				Animation.RELATIVE_TO_SELF, pivotXValue, Animation.RELATIVE_TO_SELF,
				pivotYValue);
		mRotateDownAnim.setDuration(ROTATE_ANIM_DURATION);
		mRotateDownAnim.setFillAfter(true);
	}

	/**
	 * 设置状态
	 * 
	 * <br>Created 2014-6-9 下午5:46:55
	 * @param state 状态
	 * @author        :  huangszh
	 */
	public void setState(int state) {
		if (state == mState){
		    return ;
		}
		
		if (state == STATE_REFRESHING) {	// 显示进度
			mArrowImageView.clearAnimation();
			mArrowImageView.setVisibility(View.INVISIBLE);
			mProgressBar.setVisibility(View.VISIBLE);
		} else {	// 显示箭头图片
			mArrowImageView.setVisibility(View.VISIBLE);
			mProgressBar.setVisibility(View.INVISIBLE);
		}
		
		switch(state){
		case STATE_NORMAL:
			if (mState == STATE_READY) {
				mArrowImageView.startAnimation(mRotateDownAnim);
			}
			if (mState == STATE_REFRESHING) {
				mArrowImageView.clearAnimation();
			}
			mHintTextView.setText(R.string.xlistview_header_hint_normal);
			break;
		case STATE_READY:
			if (mState != STATE_READY) {
				mArrowImageView.clearAnimation();
				mArrowImageView.startAnimation(mRotateUpAnim);
				mHintTextView.setText(R.string.xlistview_header_hint_ready);
			}
			break;
		case STATE_REFRESHING:
			mHintTextView.setText(R.string.xlistview_header_hint_loading);
			break;
			default:
		}
		
		mState = state;
	}
	
	/**
	 * 设置可见高度
	 * 
	 * <br>Created 2014-6-9 下午5:47:21
	 * @param height 高度
	 * @author        :  cyw
	 */
	public void setVisiableHeight(int height) {
		if (height < 0){
			height = 0;
		}
		LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams) mContainer
				.getLayoutParams();
		lp.height = height;
		mContainer.setLayoutParams(lp);
	}

	/**
	 * 获取可见高度
	 * 
	 * <br>Created 2014-6-9 下午5:45:42
	 * @return int
	 * @author        :  cyw
	 */
	public int getVisiableHeight() {
		return mContainer.getHeight();
	}

}
